<template>
<el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
  <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="品牌名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="品牌名称"></el-input>
    </el-form-item>
    <el-form-item label="品牌logo" prop="logo">
      <upload ref="upload" @refreshDataList="handleUploadResult"></upload>
    </el-form-item>
    <el-form-item label="品牌描述" prop="descript">
      <el-input v-model="dataForm.descript" placeholder="品牌描述"></el-input>
    </el-form-item>
    <el-form-item label="是否显示" prop="showStatus">
      <el-switch v-model="dataForm.showStatus" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0">
      </el-switch>
    </el-form-item>
    <el-form-item label="首字母" prop="firstLetter">
      <el-input v-model="dataForm.firstLetter" placeholder="首字母"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model.number="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="visible = false">取消</el-button>
    <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
  </span>
</el-dialog>
</template>

<script>
import Upload from '../oss/oss-upload nodialog'
export default {
  data() {
    return {
      visible: false,
      uploadVisible: false,
      dataForm: {
        brandId: 0,
        name: '',
        logo: '',
        descript: '',
        showStatus: '',
        firstLetter: '',
        sort: '',
      },
      dataRule: {
        name: [{
          required: true,
          message: '品牌名称不能为空',
          trigger: 'blur'
        }],
        logo: [{
          required: true,
          message: '品牌logo不能为空',
          trigger: 'blur'
        }],
        descript: [{
          required: true,
          message: '品牌描述不能为空',
          trigger: 'blur'
        }],
        showStatus: [{
          required: true,
          message: '显示状态不能为空',
          trigger: 'blur'
        }],
        firstLetter: [{
          validator: (rule, value, callback) => {
            if (!value) {
              return callback(new Error("首字母不能为空"))
            }
            if (!/^[A-Z]$/.test(value)) {
              return callback(new Error("必须输入一个大写字母"))
            }
            return callback()
          },
          trigger: 'blur'
        }],
        sort: [{
          validator: (rule, value, callback) => {
            if (!value) {
              return callback(new Error("排序字段不能为空"))
            }
            if (!Number.isInteger(value) || value < 0) {
              return callback(new Error("排序字段必须是正整数"))
            }
            if (value > 9999) {
              return callback(new Error("排序字段不能大于9999"))
            }
            return callback();
          },
          trigger: 'blur'
        }]
      }
    }
  },

  components: {
    Upload
  },

  methods: {
    init(id) {
      this.dataForm.brandId = id || 0
      this.visible = true
      this.$nextTick(() => {
        // this.$refs['dataForm'].resetFields()
        if (this.dataForm.brandId) {
          this.$http({
            url: this.$http.adornUrl(`/product/brand/info/${this.dataForm.brandId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.brand.name
              this.dataForm.logo = data.brand.logo
              this.dataForm.descript = data.brand.descript
              this.dataForm.showStatus = data.brand.showStatus
              this.dataForm.firstLetter = data.brand.firstLetter
              this.dataForm.sort = data.brand.sort
            }
            this.$refs.upload.init(false, this.dataForm.logo, "brand")
          })
        } else {
          this.$refs.upload.init(false, "", "brand")
        }

      })

    },

    handleUploadResult(urlList) {
      console.log("接收到上传完成后的结果:", urlList)
      this.dataForm.logo = urlList[0]
    },

    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/product/brand/${!this.dataForm.brandId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'brandId': this.dataForm.brandId || undefined,
              'name': this.dataForm.name,
              'logo': this.dataForm.logo,
              'descript': this.dataForm.descript,
              'showStatus': this.dataForm.showStatus,
              'firstLetter': this.dataForm.firstLetter,
              'sort': this.dataForm.sort
            })
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.visible = false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })

              //调用父组件的方法并传递数据
              this.$emit('refreshDataList')
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
